package logic

import (
	"context"
	"zxs-micro-2/zxs-micro-balance-server/data_store"
	"zxs-micro-2/zxs-micro-balance-server/utils"
	"zxs-micro-2/zxs-micro-proto/balancepb/balance"
)

func AddBalanceLogic(ctx context.Context, data *data_store.BalanceData,
	dataStore data_store.DataStoreInterface) (*balance.AddBalanceResponse, error) {
	dataStore.AddService(data)
	rsp := new(balance.AddBalanceResponse)
	rsp.ServerId = data.ServerId
	return rsp, nil
}

func BalanceHeartBeatLogic(ctx context.Context, data *data_store.BalanceData,
	dataStore data_store.DataStoreInterface) (*balance.BalanceHeartBeatResponse, error) {
	dataStore.ServerHeartBeat(data)
	rsp := new(balance.BalanceHeartBeatResponse)
	return rsp, nil
}

func CalBalanceLogic(ctx context.Context, ids []string,
	dataStore data_store.DataStoreInterface) (*balance.CalBalanceResponse, error) {
	services := dataStore.GetByIds(ids)
	choices := make([]utils.Choice, 0)
	for _, o := range services {
		choices = append(choices, o.ToChoice())
	}
	rsp := new(balance.CalBalanceResponse)
	if len(choices) == 0 {
		return rsp, nil
	}
	cer := utils.NewChooser(choices...)
	rsp.ServerId = cer.Pick()
	return rsp, nil
}
